package com.mis.service.impl.biz;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mis.entity.biz.OrderProductEntity;
import com.mis.page.biz.OrderProductPage;
import com.mis.service.biz.OrderProductServiceI;

import com.jeecg.pageModel.TreeNode;
import com.jeecg.util.pojo.MyBeanUtils;
import com.jeecg.util.dbcommon.SearchSqlGenerateUtil;
import com.jeecg.service.impl.BaseServiceImpl;
import com.jeecg.pageModel.DataGrid;
import com.jeecg.dao.jdbc.JdbcDao;
import com.jeecg.dao.BaseDaoI;

/**   
 * @Title: ServiceImpl
 * @Description: 订单产品明细
 * @author zhangdaihao
 * @date 2014-12-08 17:21:53
 * @version V1.0   
 *
 */
@Service("orderProductService")
public class OrderProductServiceImpl extends BaseServiceImpl implements OrderProductServiceI {

	@Autowired
	//SQL 使用JdbcDao
	private JdbcDao jdbcDao;
	private BaseDaoI<OrderProductEntity> orderProductEntityDao;

	public BaseDaoI<OrderProductEntity> getOrderProductEntityDao() {
		return orderProductEntityDao;
	}
	@Autowired
	public void setOrderProductEntityDao(BaseDaoI<OrderProductEntity> orderProductEntityDao) {
		this.orderProductEntityDao = orderProductEntityDao;
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	public DataGrid datagrid(OrderProductPage orderProductPage) {
		DataGrid j = new DataGrid();
		j.setRows(getPagesFromEntitys(find(orderProductPage)));
		j.setTotal(total(orderProductPage));
		return j;
	}

	private List<OrderProductPage> getPagesFromEntitys(List<OrderProductEntity> orderProductEntitys) {
		List<OrderProductPage> orderProductPages = new ArrayList<OrderProductPage>();
		if (orderProductEntitys != null && orderProductEntitys.size() > 0) {
			for (OrderProductEntity tb : orderProductEntitys) {
				OrderProductPage b = new OrderProductPage();
				BeanUtils.copyProperties(tb, b);
				orderProductPages.add(b);
			}
		}
		return orderProductPages;
	}

	private List<OrderProductEntity> find(OrderProductPage orderProductPage) {
		String hql = "from OrderProductEntity t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(orderProductPage, hql, values);

		if (orderProductPage.getSort() != null && orderProductPage.getOrder() != null) {
			hql += " order by " + orderProductPage.getSort() + " " + orderProductPage.getOrder();
		}
		return orderProductEntityDao.find(hql, orderProductPage.getPage(), orderProductPage.getRows(), values);
	}

	private Long total(OrderProductPage orderProductPage) {
		String hql = "select count(*) from OrderProductEntity t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(orderProductPage, hql, values);
		return orderProductEntityDao.count(hql, values);
	}

	private String addWhere(OrderProductPage orderProductPage, String hql, List<Object> values) {
	    //循环查询条件Page的所有[Integer][String]类型的字段，如果字段不为空则动态加上查询条件
		//-----------------------------------------------------
		StringBuffer hqlbf = new StringBuffer(hql);
		
		OrderProductEntity orderProductEntity = new OrderProductEntity();
		BeanUtils.copyProperties(orderProductPage, orderProductEntity);
		SearchSqlGenerateUtil.createSearchParamsHql(hqlbf, values, orderProductEntity);
		hql = hqlbf.toString();
		return hql;
	}

	public void add(OrderProductPage orderProductPage) {
		OrderProductEntity t = new OrderProductEntity();
		BeanUtils.copyProperties(orderProductPage, t);
		orderProductEntityDao.save(t);
	}

	public void update(OrderProductPage orderProductPage) throws Exception {
		OrderProductEntity t = orderProductEntityDao.get(OrderProductEntity.class, orderProductPage.getObid());
	    if(t != null) {
			MyBeanUtils.copyBeanNotNull2Bean(orderProductPage, t);
		}
	}

	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				String hql = "from OrderProductEntity where obid = '"+id+"'";
				OrderProductEntity t = orderProductEntityDao.get(hql);
				if (t != null) {
					orderProductEntityDao.delete(t);
				}
			}
		}
	}

	public OrderProductEntity get(OrderProductPage orderProductPage) {
		return orderProductEntityDao.get(OrderProductEntity.class, orderProductPage.getObid());
	}

	public OrderProductEntity get(java.lang.String obid) {
		return orderProductEntityDao.get(OrderProductEntity.class, obid);
	}
	public List<OrderProductEntity> listAll(OrderProductPage orderProductPage) {
		String hql = "from OrderProductEntity where 1 = 1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(orderProductPage, hql, values);
		List<OrderProductEntity> list = orderProductEntityDao.find(hql,values);
		return list;
	}
}
